//
//  LoginViewModel.m
//  Rac
//
//  Created by 崔博 on 2018/7/5.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "LoginViewModel.h"
#import "NetMannger.h"
@implementation LoginViewModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        RACSignal * userNameLengthSig = [RACObserve(self, userName) map:^id _Nullable(NSString * value) {
            
            if (value.length<6) {
                return @(YES);
            }else
            {
                return @(NO);
            }
        }];
        RACSignal *passwordLengthSig = [RACObserve(self, password)
                                        map:^id(NSString *value) {
                                            if (value.length > 6) return @(YES);
                                            return @(NO);
                                        }];
        
        RACSignal *loginBtnEnable = [RACSignal combineLatest:@[userNameLengthSig, passwordLengthSig] reduce:^id(NSNumber *userName, NSNumber *password){
            return @([userName boolValue] && [password boolValue]);
        }];
        
        _loginCommand = [[RACCommand alloc] initWithEnabled:loginBtnEnable signalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [NetMannger loginWithName:self.userName pwd:self.password];
        }];

    }
    return self;
}
@end
